package com.example.beat_x3.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beat_x3.R;


import java.util.ArrayList;

import Entity.Inf;
import Entity.Reservation;

public class MyReservationAdapter extends RecyclerView.Adapter<MyReservationAdapter.Holder> {

    public ArrayList<Reservation> reservations;
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(String id, boolean st, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public MyReservationAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(Inf.context).inflate(R.layout.item_myreservation, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        if (reservations == null) return 0;
        return reservations.size();
    }

    class Holder extends RecyclerView.ViewHolder {


        TextView wenben2;
        TextView wenben3;
        TextView wenben4;

        TextView wenben5;
        ImageView zhuangtai;
        String appointmentNumber;
        boolean st;
        int position;

        public Holder(@NonNull View itemView) {
            super(itemView);

            this.wenben2 = itemView.findViewById(R.id.myr_text2);
            this.wenben3 = itemView.findViewById(R.id.myr_text3);
            this.wenben4 = itemView.findViewById(R.id.myr_text4);
            this.wenben5 = itemView.findViewById(R.id.myr_text5);
            this.zhuangtai = itemView.findViewById(R.id.yuyuezhuangtai);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(appointmentNumber, st, position);
                    }
                }
            });
        }

        void bind(int position) {
            appointmentNumber = reservations.get(position).AppointmentNumber;
            wenben2.setText("场地："+reservations.get(position).PlacePosition);
            wenben3.setText(reservations.get(position).ActivityType);
            wenben4.setText("预约时间"+reservations.get(position).AppointmentTime);
            this.position = position;
            int n = reservations.get(position).AppointmentCheckStatus;
            st = n == 1 || n == 2;
            switch (n) {
                case 0:
                    zhuangtai.setImageResource(R.drawable.red);
                    wenben5.setText("未通过");
                    break;
                case 1:
                    zhuangtai.setImageResource(R.drawable.green);
                    wenben5.setText("已通过");
                    break;
                case 2:
                    zhuangtai.setImageResource(R.drawable.yellow);
                    wenben5.setText("审核中");
                    break;
                case 3:
                    zhuangtai.setImageResource(R.drawable.cancel);
                    wenben5.setText("已取消");
            }
        }
    }
}
